{include file="public/header296"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-row">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <form class="layui-form" lay-filter="layui-app-form" id="layui-app-form" style="display:none">
                            <div class="layui-form-item" style="margin-top: 20px">
                                <div class="layui-inline">
                                    <label class="layui-form-label">名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="name" id="name" lay-verify="required"
                                               class="layui-input"/>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <script type="text/html" id="table-content-list">
                            <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon">编辑</i></a>
                            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete"><i class="layui-icon">删除</i></a>
                        </script>
                        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                            <ul class="layui-tab-title">
                                <li class="layui-this">站点设置</li>
                                <li>友情链接</li>
                            </ul>
                            <div class="layui-tab-content">
                                <!-- 站点设置 -->
                                <div class="layui-tab-item layui-show">
                                    <div class="layui-card-body">
                                        <div class="layui-form" lay-filter="Lay-site-config-form" id="Lay-site-config-form">
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">站点标识</label>
                                                <div class="layui-input-inline">
                                                    <input type="text" name="site_flag" class="layui-input" value="{$data.site_flag|default=''}" />
                                                </div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">网站名称</label>
                                                <div class="layui-input-block">
                                                    <input type="text" name="site_name" class="layui-input" />
                                                </div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">网站标题</label>
                                                <div class="layui-input-block">
                                                    <input type="text" name="site_title" class="layui-input" />
                                                </div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">关键字</label>
                                                <div class="layui-input-block">
                                                    <input type="text" name="keywords" class="layui-input" />
                                                </div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">网站描述</label>
                                                <div class="layui-input-block">
                                                    <textarea name="description" class="layui-textarea"></textarea>
                                                    <p>系统限制500字符以内</p>
                                                </div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">网站域名</label>
                                                <div class="layui-input-block">
                                                    <input type="text" name="site_url" class="layui-input" />
                                                </div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">电子邮箱</label>
                                                <div class="layui-input-inline">
                                                    <input type="text" name="email" class="layui-input" />
                                                </div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">手机/微信</label>
                                                <div class="layui-input-inline">
                                                    <input type="text" name="mobile" class="layui-input" />
                                                </div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">联系地址</label>
                                                <div class="layui-input-block">
                                                    <input type="text" name="address" class="layui-input" />
                                                </div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">Icp备案号</label>
                                                <div class="layui-input-inline">
                                                    <input type="text" name="icp" class="layui-input" />
                                                </div>
                                            </div>
                                            <div class="layui-form-item">
                                                <div class="layui-input-block">
                                                    <input type="hidden" name="id" value="1">
                                                    <button class="layui-btn" lay-submit lay-filter="Lay-site-config-form-submit">保存</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <!-- 友情链接 -->
                                <div class="layui-tab-item">
                                    <div class="layui-card-body">
                                        <div style="padding-bottom: 10px;">
                                            <button class="layui-btn layuiadmin-btn-list" data-render="LAY-app-content-type2-list" data-type="weblink_add">添加</button>
                                        </div>
                                        <form class="layui-form" lay-filter="Lay-app-weblink-form" id="Lay-app-weblink-form" style="display:none">
                                            <div class="layui-form-item" style="margin-top: 20px">
                                                <div class="layui-inline">
                                                    <label class="layui-form-label">名称</label>
                                                    <div class="layui-input-inline">
                                                        <input type="text" id="weblink_name" lay-verify="required" class="layui-input"/>
                                                    </div>
                                                </div>
                                                <div class="layui-inline">
                                                    <label class="layui-form-label">链接</label>
                                                    <div class="layui-input-inline">
                                                        <input type="text" id="weblink_link_url" lay-verify="required" class="layui-input"/>
                                                    </div>
                                                </div>
                                            </div>
                                        </form>
                                        <table id="LAY-app-content-type2-list" lay-filter="LAY-app-content-type2-list"></table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['layer', 'table', 'form', 'element'], function () {
        var $ = layui.$
            , table = layui.table
            , element = layui.element
            , form = layui.form;

        $.ajax({
            url: "{:url('site/getSiteConfig')}",
            type: 'get',
            dataType: 'json',
            success: function(res) {
                console.log(res)
                form.val('Lay-site-config-form', {
                    'site_flag': res.data.site_flag,
                    'site_name': res.data.site_name,
                    'site_title': res.data.site_title,
                    'keywords': res.data.keywords,
                    'description': res.data.description,
                    'site_url': res.data.site_url,
                    'email': res.data.email,
                    'mobile': res.data.mobile,
                    'address': res.data.address,
                    'icp': res.data.icp,
                    'id': res.data.id
                });

                form.render();
            }
        });

        // 监听提交
        form.on('submit(Lay-site-config-form-submit)', function(data){
            var field = data.field;
            $.post("{:url('site/edit')}", {field:field}, function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1, time: 3000});
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            });
            return false;
        });

        // 监控tab标签
        element.on('tab(docDemoTabBrief)', function (data) {
            var index = data.index;
            switch (index) {
                case 1:
                    table.render({
                        elem: '#LAY-app-content-type2-list'
                        , url: "{:url('weblink/getWeblinkList')}"
                        , request: {
                            pageName: 'page',
                            limitName: 'page_size'
                        }
                        , page: {
                            layout: ['limit', 'count', 'prev', 'page', 'next']
                            , first: '首页'
                            , last: '尾页'
                            , limit: 10
                        }
                        , cellMinWidth: 120
                        , cols: [[
                            {field: 'id', title: "ID", width: 80},
                            {field: 'name', title: "名称", },
                            {field: 'link_url', title: "链接", },
                            {fixed: 'right', title: '操作', minWidth: 120, toolbar: '#table-content-list'}
                        ]]
                        , text: {
                            none: '暂无相关数据'
                        }
                    });
                    break;
            }
        });

        // 监听行工具事件
        table.on('tool(LAY-app-content-type2-list)', function (obj) {
            var data = obj.data
                , layEvent = obj.event;
            if (layEvent == 'delete') {
                layer.confirm("是否删除此条内容？", function () {
                    $.post("{:url('weblink/delete')}", {id: data.id}, function (res) {
                        if (res.code == 0) {
                            obj.del();
                            layer.msg(res.msg, {icon: 1});
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    });
                });
            } else if (layEvent == 'edit') {
                $("#weblink_name").val(data.name);
                $("#weblink_link_url").val(data.link_url);
                form.render();
                layer.open({
                    type: 1,
                    skin: 'demo-class',
                    area: ['400px', '300px'],
                    title: '编辑',
                    content: $("#Lay-app-weblink-form"),
                    shade: 0,
                    btn: ['确定', '取消'],
                    yes: function () {
                        var name = $('#weblink_name').val();
                        var link_url = $('#weblink_link_url').val();
                        if (name == '') {
                            return layer.msg('名称不能为空', {icon: 5});
                        }
                        $.post("{:url('weblink/edit')}", {id: data.id, name: name, link_url: link_url}, function (res) {
                            if (res.code == 0) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('LAY-app-content-type2-list');
                                $("#Lay-app-weblink-form")[0].reset();
                                layer.closeAll();
                            } else {
                                layer.msg(res.msg, {icon: 5});
                            }
                        });
                    },
                    btn2: function () {
                        $("#Lay-app-weblink-form")[0].reset();
                        layer.closeAll();
                    },
                    cancel: function () {
                        $("#Lay-app-weblink-form")[0].reset();
                        layer.closeAll();
                    }
                });
            }
        });

        var $ = layui.$, active = {
            add: function () {
                var type = $(this).data('id');
                var render = $(this).data('render');
                layer.open({
                    type: 1,
                    skin: 'demo-class',
                    area: ['400px', '200px'],
                    title: "添加",
                    content: $("#layui-app-form"),
                    shade: 0,
                    btn: ['确定', '取消'],
                    yes: function () {
                        var name = $('#name').val();
                        if (name == '') {
                            layer.msg('名称不能为空', {icon: 5});
                            return false;
                        }
                        $.post("{:url('config/edit')}", {type: type, name: name}, function (res) {
                            if (res.code == 0) {
                                layer.msg(res.msg, {icon: 1});
                                $("#layui-app-form")[0].reset();
                                table.reload(render);
                                layer.closeAll();
                            } else {
                                layer.msg(res.msg, {icon: 5});
                            }
                        });
                    },
                    btn2: function () {
                        $("#layui-app-form")[0].reset();
                        layer.closeAll();
                    },
                    cancel: function () {
                        $("#layui-app-form")[0].reset();
                        layer.closeAll();
                    }
                });
            },
            weblink_add: function () {
                var render = $(this).data('render');
                layer.open({
                    type: 1,
                    skin: 'demo-class',
                    area: ['400px', '300px'],
                    title: "添加",
                    content: $("#Lay-app-weblink-form"),
                    shade: 0,
                    btn: ['确定', '取消'],
                    yes: function () {
                        var name = $('#weblink_name').val();
                        var link_url = $('#weblink_link_url').val();
                        if (name == '') {
                            layer.msg('名称不能为空', {icon: 5});
                            return false;
                        }
                        $.post("{:url('weblink/insert')}", {name: name, link_url: link_url}, function (res) {
                            if (res.code == 0) {
                                layer.msg(res.msg, {icon: 1});
                                $("#Lay-app-weblink-form")[0].reset();
                                table.reload(render);
                                layer.closeAll();
                            } else {
                                layer.msg(res.msg, {icon: 5});
                            }
                        });
                    },
                    btn2: function () {
                        $("#Lay-app-weblink-form")[0].reset();
                        layer.closeAll();
                    },
                    cancel: function () {
                        $("#Lay-app-weblink-form")[0].reset();
                        layer.closeAll();
                    }
                });
            },
        };

        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
{include file="public/footer"}